package com.shuhua.taskServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuhua.common.result.Result;
import com.shuhua.pojo.dto.BankCardDTO;
import com.shuhua.pojo.entity.BankCard;
import com.shuhua.pojo.vo.BankCardVO;
import com.shuhua.taskServer.mapper.BankCardMapper;
import com.shuhua.taskServer.service.BankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BankCardServicelmpl implements BankCardService {

@Autowired private BankCardMapper bankCardMapper;


    @Override
    public List<BankCardVO> getCardsByUserId(Integer craftsmanId) {
        
        // 1. 从数据库查询银行卡实体列表
        List<BankCard> bankCards = bankCardMapper.selectCardsByUserId(craftsmanId);

        // 2. 将实体列表转换为VO列表（数据脱敏）
        return convertToVOList(bankCards);
    }

    @Override
    public Result addBankCard(BankCardDTO bankCardDTO) {
        BankCard bankCard = new BankCard();
        bankCard.setCraftsmanId(bankCardDTO.getCraftsmanId());
        bankCard.setCardNumber(bankCardDTO.getCardNumber());
        bankCard.setBankName(bankCardDTO.getBankName());
        bankCard.setRealName(bankCardDTO.getRealName());
        bankCard.setCardState(bankCardDTO.getCardState());
        
     if (  bankCardMapper.insert(bankCard) > 0) {
        return Result.success();
     } else {
        return Result.error();
     }
     
    }


    @Override
    public  Result getdetail (BankCardDTO bankCardDTO) {
        if (bankCardDTO.getCardNumber() == null || bankCardDTO.getCardNumber().isEmpty()) {
            return Result.error();
        }

        if (bankCardDTO.getCraftsmanId() == null || bankCardDTO.getCraftsmanId() <= 0) {
            return Result.error();
        }

        // 创建安全的查询条件
        LambdaQueryWrapper<BankCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankCard::getCardNumber, bankCardDTO.getCardNumber())
                .eq(BankCard::getCraftsmanId, bankCardDTO.getCraftsmanId());

        // 执行查询
        BankCard bankCard = bankCardMapper.selectOne(queryWrapper);

        if (bankCard == null) {

            return Result.error("银行卡不存在或无权访问");
        }

        // 转换为安全的VO对象
        BankCardVO cardVO = convertToVO(bankCard);
        return Result.success(cardVO);
    }

    @Override
    public Result delete(BankCardDTO bankCardDTO) {
        LambdaQueryWrapper<BankCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankCard::getCraftsmanId, bankCardDTO.getCraftsmanId())
                .eq(BankCard::getCardNumber, bankCardDTO.getCardNumber());

        // 执行删除（物理删除）
        int rows = bankCardMapper.delete(queryWrapper);

        if (rows > 0) {

            return Result.success("删除成功");
        } else {

            return Result.error("银行卡不存在或已被删除");
        }
    }


    private List<BankCardVO> convertToVOList(List<BankCard> bankCards) {
        return bankCards.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }



    private BankCardVO convertToVO(BankCard card) {
        BankCardVO vo = new BankCardVO();

        // 复制安全字段
        vo.setCraftsmanId(card.getCraftsmanId());
        vo.setBankName(card.getBankName());
        vo.setRealName(card.getRealName());
        vo.setCardState(card.getCardState());
        vo.setIsDefault(card.getIsDefault());
        vo.setCreateAt(card.getCreateAt());

        // 处理敏感数据 - 只显示卡号后4位
        String maskedNumber = maskCardNumber(card.getCardNumber());
        vo.setCardNumber(maskedNumber);

        // 其他字段转换...
        // vo.setBindDate(card.getBindDate());
        return vo;
    }

    // 卡号脱敏工具方法
    private String maskCardNumber(String fullCardNumber) {
        if (fullCardNumber == null || fullCardNumber.length() < 4) {
            return "****";
        }
        String lastFour = fullCardNumber.substring(fullCardNumber.length() - 4);
        return "**** ​**** ​**** " + lastFour;
    }

    


}
